package com.gl.emms.whale.ui;

import java.util.Timer;
import java.util.TimerTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import com.gl.emms.client.android.EMMSPushManager;
import com.gl.emms.nio.mutual.ReplyBody;
import com.gl.emms.whale.R;
import com.gl.emms.whale.app.Constant;
import com.gl.emms.whale.app.MyApplication;
import com.gl.emms.whale.app.WhaleMonitorActivity;
import com.gl.emms.whale.util.DialogFactory;
import com.gl.emms.whale.util.MD5;

/**修改密码界面类
 * @author ganlinux
 *
 */
public class ChangePasswordActivity extends WhaleMonitorActivity implements OnClickListener{
	Timer timer;//定时器

	private EditText oldPwdEdit;//原密码
	private EditText newPwdEdit; //新密码
	private EditText renewPwdEdit;//重复输入新密码
	private Button changePwdBtn;//修改密码按钮
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉标题栏
		setContentView(R.layout.activity_changepassword);

		initViews();
	}
	/**
	 * 初始化界面
	 * @time 2014年11月13日上午11:26:53
	 * @return void
	 * @user ganlinux
	 */
	@Override
	public void initViews() {
		Button titleBackBtn = (Button)findViewById(R.id.TitleBackBtn);  
		titleBackBtn.setOnClickListener(this);

		oldPwdEdit = (EditText)findViewById(R.id.old_password);
		newPwdEdit = (EditText)findViewById(R.id.new_password);
		renewPwdEdit = (EditText)findViewById(R.id.renew_password);
		changePwdBtn = (Button) findViewById(R.id.change_pwd_btn);
		changePwdBtn.setOnClickListener(this);
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {

		case R.id.change_pwd_btn://修改密码
			doChangePwd();
			break;
		case R.id.TitleBackBtn://返回
			//timerCancle();
			new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_BACK);
			finish();
			break;
		}
	}
	/**
	 * 修改密码函数
	 * @time 2014年11月13日上午11:27:17
	 * @return void
	 * @user ganlinux
	 */
	public void doChangePwd() {

		if ((!"".equals(oldPwdEdit.getText().toString().trim()))&&(!"".equals(newPwdEdit.getText().toString().trim()))&&(!"".equals(renewPwdEdit.getText().toString().trim()))) {
			if ((!(oldPwdEdit.getText().toString().trim()).equals(oldPwdEdit.getText().toString().trim()))) {
				showToask(getString(R.string.password_err));
				return;
			}
			String oldPwdString,newPwdString;
			oldPwdString = MD5.getMD5(oldPwdEdit.getText().toString());
			newPwdString = MD5.getMD5(newPwdEdit.getText().toString());
			showProgressDialog(getString(R.string.progress_dialog_title), getString(R.string.progress_dialog_context));
			EMMSPushManager.requestChangePassword(ChangePasswordActivity.this,MyApplication.getUserData().getIdIP(),MyApplication.getUserData().getName(),oldPwdString, newPwdString);
			//startTimer(30);
		}else
		{
			showToask(getString(R.string.pwdempty_tips));
		}
	}
	@Override
	public void onReplyReceived(final ReplyBody reply) {
		super.onReplyReceived(reply);
		if (reply.getType().equals(Constant.TYPE_CHANGEPASSWORD_RESPONSE)) {
			//timerCancle();
			hideProgressDialog();
			String changepwd_state = reply.get(Constant.CHANGEPASSWORD_STATE);
			if(changepwd_state.equals(Constant.CHANGEPASSWORD_SUCCESS))
			{
				DialogFactory.ToastDialog(ChangePasswordActivity.this,"修改密码",
						"成功");
			}else if(changepwd_state.equals(Constant.CHANGEPASSWORD_FAIL_NOUSER))
			{
				DialogFactory.ToastDialog(ChangePasswordActivity.this,"修改密码",
						"失败,无此用户");
			}else if(changepwd_state.equals(Constant.CHANGEPASSWORD_FAIL_WRONG))
			{
				DialogFactory.ToastDialog(ChangePasswordActivity.this,"修改密码",
						"失败，原密码输入有误");
			}


		}

	}
	/**启动定时器
	 * @param secends
	 */
	public void startTimer(int secends){
		TimerTask task = new TimerTask(){   
			public void run() {
				timerCancle();
				timerStop();
			}   
		};
		timer = new Timer(true);  
		timer.schedule(task,secends*1000);
	}
	/**
	 * 取消定时器
	 */
	public void timerCancle(){
		timer.cancel();
	}
	/**
	 * 终止定时器
	 */
	public void timerStop(){
		Log.v("whale", "timerStop");

		Message message = new Message();      
		message.what = 0x02;      
		handler.sendEmptyMessage(message.what);
	}
	private Handler handler = new Handler(){  
		public void handleMessage(Message msg) {  
			switch (msg.what) {      
			case 0x02: 
				hideProgressDialog();
				DialogFactory.ToastDialog(ChangePasswordActivity.this,"修改密码",
						"失败，通讯失败，请重试");
				break;      
			}      
			super.handleMessage(msg);  
		}    
	}; 
}